
export const BASE_URL = "http://localhost:8080";

export const request = ({ url, method, header, data }) => {
  return new Promise((resolve, reject) => {
    const token = uni.getStorageSync('Authorization'); //获取token
    uni.request({
      url: `${BASE_URL}${url}`,
      method,
      header: {
        ...header,
        Authorization: url =='/auth/login' ? "" : token //判断是否是login，是则添加Authorization
      },
      data,
      success: (res) => {
        // if (res.data.code !== 200 && res.data.code !== 201) {
        //   return reject("服务器返回错误");
        // }
        if (res.data.code === 200 || res.data.code === 201) {
          return resolve(res.data);
        } 
		// else if (res.data.code === 500) {
  //         uni.showToast({
  //           title: "登录已过期，请重新登录",
  //           icon: "none",
  //           duration: 2000,
  //           complete: () => {
  //             uni.clearStorage();
  //             uni.navigateTo({
  //               url: "/pages/login/index",
  //             });
  //           },
  //         });
  //       }
		else if (res.data.status === 500) {
          uni.showToast({
            title: "登录已过期，请重新登录",
            icon: "none",
            duration: 2000,
            complete: () => {
              uni.clearStorage();
              uni.navigateTo({
                url: "/pages/login/index",
              });
            },
          });
        } else {
          return reject(res.data.message);
        }
      },
      fail: (e) => {
        console.log(e);
        uni.showToast({
          title: "服务器异常，请联系管理员",
          icon: "none",
        });
        return reject(e.errMsg);
      },
    });
  });
};
